﻿// ParseLib - An open source library for natural language processing.
// Copyright (C) Kais Dukes, 2012. E-mail: kais@kaisdukes.com
// http://parselib.org

using System.Text;

namespace ParseLib.Arabic.Corpora
{
    public class Location
    {
        private readonly int _key;

        public Location(
            int chapterNumber,
            int verseNumber = 0,
            int tokenNumber = 0,
            int segmentNumber = 0)
        {
            ChapterNumber = chapterNumber;
            VerseNumber = verseNumber;
            TokenNumber = tokenNumber;
            SegmentNumber = segmentNumber;

            // Human-readable key.
            _key = ChapterNumber*10000000
                   + VerseNumber*1000
                   + TokenNumber*10
                   + SegmentNumber;
        }

        public int ChapterNumber { get; private set; }
        public int VerseNumber { get; private set; }
        public int TokenNumber { get; private set; }
        public int SegmentNumber { get; private set; }

        public override string ToString()
        {
            // Start location.
            var text = new StringBuilder();
            text.Append('(');

            // Chapter number.
            text.Append(ChapterNumber);

            // Verse number.
            text.Append(':');
            if (VerseNumber > 0)
            {
                text.Append(VerseNumber);
            }

            // Token number.
            if (TokenNumber > 0)
            {
                text.Append(':');
                text.Append(TokenNumber);
            }

            // Segment number.
            if (SegmentNumber > 0)
            {
                text.Append(':');
                text.Append(SegmentNumber);
            }

            // Return location.
            text.Append(')');
            return text.ToString();
        }

        public static Location Parse(string text)
        {
            try
            {
                // Brackets?
                if (text.StartsWith("(") && text.EndsWith(")"))
                {
                    text = text.Substring(1, text.Length - 2);
                }

                // Parse.
                var items = text.Split(':');
                var chapterNumber = int.Parse(items[0]);
                var verseNumber = items.Length > 1 ? int.Parse(items[1]) : 0;
                var tokenNumber = items.Length > 2 ? int.Parse(items[2]) : 0;
                var segmentNumber = items.Length > 3 ? int.Parse(items[3]) : 0;
                return new Location(chapterNumber, verseNumber, tokenNumber, segmentNumber);
            }
            catch
            {
                throw new ParseLibException("Failed to parse location '{0}'.", text);
            }
        }

        public override int GetHashCode()
        {
            return _key;
        }

        public override bool Equals(object value)
        {
            return value != null && _key == ((Location) value)._key;
        }
    }
}